package com.sfpay.merchant.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfpay.merchant.auth.entity.system.SysDictData;
import com.sfpay.merchant.auth.entity.system.SysDictType;
import com.sfpay.merchant.auth.mapper.SysDictDataMapper;
import com.sfpay.merchant.auth.mapper.SysDictTypeMapper;
import com.sfpay.merchant.auth.service.ISysDictTypeService;
import com.sfpay.merchant.auth.utils.DictUtils;
import com.sfpay.merchant.common.constant.UserConstants;
import com.sfpay.merchant.common.core.domain.Ztree;
import com.sfpay.merchant.common.core.text.Convert;
import com.sfpay.merchant.common.exception.ServiceException;
import com.sfpay.merchant.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author fangchen
 * @since 2022-08-17
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {

    @Autowired
    private  SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private ISysDictTypeService dictTypeService;


    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        QueryWrapper<SysDictData> queryWrapper=new QueryWrapper();
        queryWrapper.lambda().eq(SysDictData::getDictType,dictType);
        dictDatas = sysDictDataMapper.selectList(queryWrapper);
        if (StringUtils.isNotEmpty(dictDatas)) {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    @Override
    public List<SysDictType> selectDictTypeList(SysDictType dictType) {
        return sysDictTypeMapper.selectDictTypeList(dictType);
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public String checkDictTypeUnique(SysDictType dict) {
        Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        SysDictType dictType = sysDictTypeMapper.checkDictTypeUnique(dict.getDictType());
        if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) {
            return UserConstants.DICT_TYPE_NOT_UNIQUE;
        }
        return UserConstants.DICT_TYPE_UNIQUE;
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dict 字典类型信息
     * @return 结果
     */
    @Override
    public int insertDictType(SysDictType dict) {
        int row = sysDictTypeMapper.insertDictType(dict);
        if (row > 0) {
            DictUtils.setDictCache(dict.getDictType(), null);
        }
        return row;
    }

    @Override
    public SysDictType selectDictTypeById(Long dictId) {
        return sysDictTypeMapper.selectDictTypeById(dictId);
    }

    /**
     * 修改保存字典类型信息
     * @param dict 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDictType(SysDictType dict) {
        SysDictType oldDict = sysDictTypeMapper.selectDictTypeById(dict.getDictId());
        sysDictTypeMapper.updateDictDataType(oldDict.getDictType(), dict.getDictType());
        int row = sysDictTypeMapper.updateDictType(dict);
        if (row > 0) {
            List<SysDictData> dictDatas = sysDictDataMapper.selectDictDataByType(dict.getDictType());
            DictUtils.setDictCache(dict.getDictType(), dictDatas);
        }
        return row;
    }

    @Override
    public void deleteDictTypeByIds(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            SysDictType dictType = selectDictTypeById(dictId);
            if (sysDictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
            sysDictDataMapper.deleteDictTypeById(dictId);
            DictUtils.removeDictCache(dictType.getDictType());
        }
    }

    @Override
    public void resetDictCache() {
        clearDictCache();
        loadingDictCache();
    }

    @Override
    public List<SysDictType> selectDictTypeAll() {
        return sysDictTypeMapper.selectDictTypeAll();
    }

    @Override
    public SysDictType selectDictTypeByType(String dictType) {
        return sysDictTypeMapper.selectDictTypeByType(dictType);
    }

    @Override
    public List<Ztree> selectDictTree(SysDictType sysDictType) {
        List<Ztree> ztrees = dictTypeService.selectDictTree(new SysDictType());
        return ztrees;
    }

    /**
     * 清空字典缓存数据
     */
    @Override
    public void clearDictCache() {
        DictUtils.clearDictCache();
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache() {
        List<SysDictType> dictTypeList = sysDictTypeMapper.selectDictTypeAll();
        for (SysDictType dict : dictTypeList) {
            List<SysDictData> dictDatas = sysDictDataMapper.selectDictDataByType(dict.getDictType());
            DictUtils.setDictCache(dict.getDictType(), dictDatas);
        }
    }


}
